import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:work_tools/utils/cache_util.dart';
import 'decrypt_logic.dart';

class DecryptPage extends StatelessWidget {
  final logic = Get.put(DecryptLogic());
  final state = Get.find<DecryptLogic>().state;

  DecryptPage({super.key});

  void _incrementCounter() {
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("解密")),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              margin: const EdgeInsets.only(left: 16, right: 16, top: 10),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(2),
                border: Border.all(width: 1, color: const Color(0xFFDFDFDD)),
                color: Colors.white,
              ),
              child: TextField(
                buildCounter: (
                    BuildContext context, {
                      required int currentLength,
                      required int? maxLength,
                      required bool isFocused,
                    }) {
                  return Padding(
                    padding: const EdgeInsets.only(bottom: 10),
                    child: Text(
                      "$currentLength/$maxLength",
                      style: const TextStyle(
                        fontSize: 14,
                        color: Color(0xFFAAAFB5),
                      ),
                    ),
                  );
                },
                scrollPadding: EdgeInsets.zero,
                controller: state.tec,
                focusNode: state.fn,
                maxLength: 3000,
                maxLines: 8,
                minLines: 1,
                style: const TextStyle(
                  fontSize: 14,
                  color: Color.fromRGBO(102, 105, 109, 1),
                ),
                decoration: InputDecoration(
                  isCollapsed: true,
                  prefix: const Text("密文: "),
                  suffixIcon: TextButton(onPressed: () => decrypt(), child: const Text("解密")),
                  hintStyle: const TextStyle(color: Color(0xFFAAAFB5), fontSize: 14),
                  hintText: "请输入加密内容",
                  contentPadding: const EdgeInsets.only(top: 8, bottom: 5, left: 13, right: 8,),
                  border: const OutlineInputBorder(borderSide: BorderSide.none),
                ),
                inputFormatters: [FilteringTextInputFormatter.deny(state.regexp)],
              ),
            ),
            Expanded(
              child: Container(
                padding: const EdgeInsets.all(40),
                child: Obx(() => Text(state.resData.value)),
              ),
            ),
            Obx(() {
              return Offstage(
                offstage: state.resData.value.isEmpty,
                child: Row(
                  children: [
                    const Spacer(),
                    TextButton(
                      onPressed: () => resetValue(),
                      child: const Text(
                        "重置",
                        style: TextStyle(
                          color: Colors.black87,
                          fontSize: 14,
                        ),
                      ),
                    ),
                    const SizedBox(width: 40),
                    TextButton(
                      onPressed: () => copyValue(),
                      child: const Text(
                        "复制",
                        style: TextStyle(
                          color: Colors.black87,
                          fontSize: 14,
                        ),
                      ),
                    ),
                    const Spacer(),
                  ],
                ),
              );
            }),
            const SizedBox(height: 40),
          ],
        ),
      ),
    );
  }

  void decrypt() {
    final value = state.tec.text;
    if (value.isEmpty) {
      print("解密内容为空");
      return;
    }
    final result = CacheUtil.decrypt(value) ?? "";
    if (result.isEmpty) {
      print("解密失败");
      return;
    }
    state.resData(result);
  }

  void resetValue() {
  }
  void copyValue() {
    Clipboard.setData(ClipboardData(text: state.resData.value));
  }
}
